package cn.otrue.patienthealthmanager.view;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.utils.CommonUtil;

public class SwitchListButton extends RelativeLayout {

	private Context context;

	private LinearLayout ll_switch_list;
	private View main;
	private int cid = 0;

	private LayoutInflater inflater;

	public SwitchListButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initView();
	}

	private void initView() {
		inflater = LayoutInflater.from(context);
		main = inflater.inflate(R.layout.item_switch_list, this);
		ll_switch_list = (LinearLayout) main.findViewById(R.id.ll_switch_list);

	}

	SwitchButtonListClickListener listclick;

	public void initButton(List<String> buttonList, int width, int height,
			int[] padding, final SwitchButtonListClickListener listclick) {
		this.listclick = listclick;
		ll_switch_list.removeAllViews();

		if (width >= 0) {
			width = CommonUtil.dip2px(context, width);
		}

		if (height >= 0) {
			height = CommonUtil.dip2px(context, height);
		}

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,
				height);

		for (int i = 0; i < buttonList.size(); i++) {

			View view = new View(context);
			view.setBackgroundResource(R.color.main);
			LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(
					 CommonUtil.dip2px(context, 2), LayoutParams.MATCH_PARENT);

			TextView eachView = (TextView) inflater.inflate(
					R.layout.item_switch_text, null);
			if (padding != null) {
				eachView.setPadding(padding[0], padding[1], padding[2],
						padding[3]);
			}
			eachView.setTag(i);
			eachView.setText(buttonList.get(i));
			eachView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {

					setClicked(Integer.parseInt(arg0.getTag().toString()));
					listclick.onGetClicked(Integer.parseInt(arg0.getTag()
							.toString()));
				}
			});

			ll_switch_list.addView(eachView, params);
			if (i != (buttonList.size() - 1)) {
				ll_switch_list.addView(view, lineParams);
			}
		}
	}

	public void loadData() {
		listclick.onGetClicked(cid);
	}

	public void setClicked(int id) {
		cid = id;
		for (int j = 0; j < ll_switch_list.getChildCount(); j++) {
			if (ll_switch_list.getChildAt(j) instanceof TextView) {

				TextView textView = (TextView) ll_switch_list.getChildAt(j);

				if (cid == Integer.parseInt(textView.getTag().toString())) {
					textView.setTextColor(context.getResources().getColor(
							R.color.white));
					if (j == 0) {
						textView.setBackgroundResource(R.drawable.shape_switch_text_left_on);
					} else if (j == ll_switch_list.getChildCount() - 1) {
						textView.setBackgroundResource(R.drawable.shape_switch_text_right_on);
					} else {
						textView.setBackgroundResource(R.color.main);
					}

				} else {
					textView.setTextColor(context.getResources().getColor(
							R.color.black));

					if (j == 0) {
						textView.setBackgroundResource(R.drawable.shape_switch_text_left);
					} else if (j == ll_switch_list.getChildCount() - 1) {
						textView.setBackgroundResource(R.drawable.shape_switch_text_right);
					} else {
						textView.setBackgroundResource(R.color.white);
					}
				}
			}
		}

	}

	public int getClicked() {
		return cid;
	}

}
